package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.ZdeliverylogMapper;
import cn.gson.fengboot.model.mapper.ZinvoiceMapper;
import cn.gson.fengboot.model.mapper.ZreturnedmoneylogMapper;
import cn.gson.fengboot.model.mapper.ZshipmentsMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZdeliverylogService {
    @Autowired
    ZdeliverylogMapper fpmapper;

    @Autowired
    ZdeliverylogDao fpdao;

    @Autowired
    ZdeliverylogdeliveryDao jldao;

    @Autowired
    ZdeliveryDao jhdao;

    @Autowired
    ZreturnedmoneylogMapper hkmapper;

    @Autowired
    ZorderDao dddao;

    @Autowired
    ZbarterDao hhdao;

    @Autowired
    ZshipmentsDao fhdao;

    @Autowired
    ZshipmentsMapper fhmapper;
    /*
        交付记录页面查询方法
     */
    public List<Zdeliverylog> kaipiao(String search){
        return fpmapper.kaipiao(search);
    }
    /*
        交付计划详情查询记录
     */
    public List<Zdeliverylog> jiaofuxq(String search){
        return fpmapper.jiaofuxq(search);
    }

    /*
    交付记录详情查询方法
 */
    public List<Zdeliverylogdelivery> kaipiao2(String search){
        return fpmapper.kaipiao2(search);
    }

    /*
        交付记录页面新增方法
     */
    public void add(Zdeliverylog jl){
        //新增订单表
        Zdeliverylog save = fpdao.save(jl);
        Zdelivery jf = jhdao.findById(save.getDeliveryId().getDeliveryId()).get();
        jf.setDeliveryStatic("已完成");
        jhdao.save(jf);
        //新增采购详情表
        jl.getZdeliverylogdeliveriesByDeliverylogId().forEach(pur->{
            pur.setDeliverylogId(save);
        });
        jldao.saveAll(jl.getZdeliverylogdeliveriesByDeliverylogId());


        Integer sfhk = hkmapper.sfhk(jf.getOrderId().getOrderId());
        Integer sfjf = hkmapper.sfjf(jf.getOrderId().getOrderId());
        Zorder zorder = dddao.findById(jf.getOrderId().getOrderId()).get();
        System.out.println(sfhk);
        System.out.println(sfjf);
        if(sfhk==0&&sfjf==0){
            zorder.setOrderState("已完成");
            if(zorder.getBarterId().getBarterId()!=-1){
                Zbarter zbarter = hhdao.findById(zorder.getBarterId().getBarterId()).get();
                zbarter.setBarterState("已完成");
                hhdao.save(zbarter);
            }
        }else {
            zorder.setOrderState("执行中");
        }
        dddao.save(zorder);

        Zshipments cx = fhmapper.cx(jl.getDeliveryId().getDeliveryId());
        cx.setShipmentsState("已收货");
        fhdao.save(cx);
    }

    //<!--谭方圆=>客户合并的交付记录查询-->
    public List<Zdeliverylog> tfykaipiao(Integer id1, Integer id2){
        return fpmapper.tfykaipiao(id1,id2);
    }
}
